package com.xxx.mch.controller;

import com.xxx.mch.domain.dto.UsedScoreTemplateDto;
import com.xxx.mch.domain.request.RuleSearchRequest;
import com.xxx.mch.domain.request.UsedScoreTemplateRequest;
import com.xxx.mch.service.UsedScoreService;
import com.xxx.mch.utils.WrapMapper;
import com.xxx.mch.utils.Wrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javax.annotation.Resource;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author FengHaixin
 * @description 一句话描述该文件的用途
 * @date 2020-10-12
 */
@RestController
@RequestMapping ("usedScore")
@Api(tags = "采用分接口")
public class UsedScoreController
{
    @Resource
    private UsedScoreService usedScoreService;

    @ApiOperation (value = "采用分模板列表")
    @GetMapping (value = "template", produces = MediaType.APPLICATION_JSON_VALUE)
    public Wrapper<List<UsedScoreTemplateDto>> taskTemplate(@RequestParam (value = "parentId", required = false) List<Integer> parentId,
                                                            @RequestParam (value = "depth", defaultValue = "4") Integer depth)
    {
        return WrapMapper.ok(usedScoreService.getTaskTemplate(parentId, depth, true));
    }


    @ApiOperation (value = "采用分细则搜索")
    @GetMapping (value = "rule/search", produces = MediaType.APPLICATION_JSON_VALUE)
    public Wrapper<List<UsedScoreTemplateDto>> ruleSearch(@ModelAttribute @Validated RuleSearchRequest ruleSearchRequest)
    {
        return WrapMapper.ok(usedScoreService.ruleSearch(ruleSearchRequest));
    }

    @ApiOperation (value = "创建或更新采用分模板")
    @PostMapping (value = "template", produces = MediaType.APPLICATION_JSON_VALUE)
    public Wrapper<String> createOrUpdate(@RequestBody List<UsedScoreTemplateRequest> templateRequest)
    {
        usedScoreService.createOrUpdateTemplate(templateRequest);
        return WrapMapper.ok();
    }

    @ApiOperation (value = "根据ID，删除采用分模板")
    @DeleteMapping (value = "template/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    public Wrapper<String> deleteTemplate(@PathVariable ("id") Integer id)
    {
        usedScoreService.deleteTemplate(id);
        return WrapMapper.ok();
    }
}